<?php
/**
 * Main class for module Htaccess
 * 
 * @package      WebGuru3
 * @subpackage   modules/htaccess/
 * @author       Ondrej Rafaj
 * @author       WebGuruCMS3 Framework CMS admin generator (http://www.webgurucms.com)
 * @version      1.0.0.0
 * @wgversion    3.0.0.0
 * @wgdeveloper  1.0.0.0
 * @since        17. February 2009
 */

class moduleHtaccess extends dbModelHtaccess {
	
	public $name            = NULL;
	public $version         = NULL;
	public $author          = NULL;
	
	private static $_path   = NULL;
	private static $_module = NULL;
	
	
	public function __construct() {
		$this->_init();
	}
	
	
	private function _init() {
		$this->name    = 'Htaccess';
		$this->code    = 'htaccess';
		$this->version = '0.0.0.1';
		$this->author  = 'Ondrej Rafaj';
		
		$this->_module = dbSystem::getModulesByName($this->code);
		$this->_path   = wgPaths::getAdminPath().$this->_module['part'].'/';
		
	}
	
	// ------------------------- class functions -------------------------
	
	public static function compileHtaccess($web=NULL) {
		if (!(bool) $web) $web = wgSystem::getCurrentWebsite();
		$data = self::getHeader();
		$mPag = wgModules::runModule('pages');
		$arr = HtaccessRowsModel::getRows($web);
		foreach ($arr as $row) $data .= '# Rule: '.$row->getName()."\r\n".$row->getRule()."\r\n\r\n";
		$arr = HtaccessCodeModel::getCodes($web);
		foreach ($arr as $row) $data .= '# Code part: '.$row->getName()."\r\n".$row->getCode()."\r\n\r\n";
		$arr = HtaccessHtpasswdModel::getLocations($web);
		foreach ($arr as $row) $data .= '# Protected area: '.$row->getName()."\r\n# ".$row->getLocation()."\r\n\r\n";
		$data .= '';
		$data = str_ireplace('%%', '$', $data);
		return $data;
	}
	
	public static function getContent($path=NULL) {
		$path = self::_getPath($path);
		if (!file_exists($path)) return false;
		return wgIo::readFile($path);
	}
	
	public static function backupFile($path=NULL) {
		$path = self::_getPath($path);
		if (!file_exists($path)) return false;
		$info = pathinfo($path);
		return wgIo::copy($path, $info['dirname'].'/'.$info['basename'].'.'.date('Y-m-d@H-i-s').'.bak');
	}
	
	public static function writeContent($content=NULL, $path=NULL) {
		$path = self::_getPath($path);
		wgModules::runModule('pages');
		return wgIo::writeFile($path, $content);
	}
	
	private static function _getPath($path) {
		if (empty($path)) return wgPaths::getPath().'.htaccess';
		return $path;
	}
	
	public static function getHeader() {
		$data = '# ATTENTION!!!
# DO NOT MODIFY THIS FILE OR ANY PART OF IT.
# THIS FILE IS AUTOMATICALY GENERATED BY WEBGURU CMS
# AND IS CHANGED EVERYTIME THE GENERATE BUTTON IS PRESSED
# PLEASE USE MODULE HTACCESS TO CHANGE WHAT IS NECCESSARY
# SEARCH FOR SUPPORT ON http://www.webgurucms.com

';
		if (self::isRewrite()) $data .= 'RewriteEngine on

';
		return $data;
	}
	
	public static function isRewrite() {
		return true;
	}
	
	
	
}
		
?>